<template>
	<xn-form-container
		:title="formData.id ? '编辑活动' : '创建活动'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="活动时间：" name="activityTime">
				<a-date-picker
					v-model:value="formData.activityTime"
					value-format="YYYY-MM-DD HH:mm:ss"
					show-time
					placeholder="请选择活动时间"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="活动标题：" name="activityTitle">
				<a-input v-model:value="formData.activityTitle" placeholder="请输入活动标题" allow-clear />
			</a-form-item>
			<a-form-item label="活动类型：" name="activityType">
				<a-select v-model:value="formData.activityType" placeholder="请选择活动类型" :options="activityTypeOptions" />
			</a-form-item>
			<a-form-item label="线上活动链接：" name="activityUrl" v-show="formData.activityType === '线上活动'">
				<a-input v-model:value="formData.activityUrl" placeholder="请输入线上活动地址链接" allow-clear> </a-input>
			</a-form-item>
			<a-form-item label="活动内容（通知内容）：" name="activityContent">
				<a-textarea
					v-model:value="formData.activityContent"
					placeholder="请输入活动内容"
					:auto-size="{ minRows: 3, maxRows: 5 }"
				/>
			</a-form-item>
			<a-form-item label="通知类型：" name="noticeType">
				<a-select v-model:value="formData.noticeType" placeholder="请选择通知类型" :options="noticeTypeOptions" />
			</a-form-item>

			<a-form-item label="通知范围" name="noticeArea">
				<a-radio-group v-model:value="formData.noticeArea" button-style="solid">
					<a-radio-button value="社团">社团</a-radio-button>
					<a-radio-button value="自选组织">自选组织</a-radio-button>
				</a-radio-group>
			</a-form-item>

			<a-form-item label="选择通知社团：" name="groupId" v-show="formData.noticeArea === '社团'">
				<a-select v-model:value="formData.groupId" placeholder="请选择社团" mode="multiple" :options="groupData">
				</a-select>
			</a-form-item>

			<a-form-item label="选择通知组织：" name="orgId" v-show="formData.noticeArea === '自选组织'">
				<a-select v-model:value="formData.orgId" placeholder="请选择组织" mode="multiple" :options="orgData">
				</a-select>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizActivityForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import bizActivityApi from '@/api/biz/bizActivityApi'
	import bizOrgApi from '@/api/biz/bizOrgApi'
	import bizUserGroupApi from '@/api/biz/bizUserGroupApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const activityTypeOptions = ref([])
	const noticeTypeOptions = ref([])

	const orgData = ref([])
	const groupData = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		activityTypeOptions.value = tool.dictList('ACTIVITY_TYPE')
		noticeTypeOptions.value = tool.dictList('NOTICE_TYPE')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {}

	// 获取组织结构
	const getTree = () => {
		bizOrgApi.orgTree().then((res) => {
			let data = {
				value: res[0].id,
				label: res[0].name
			}
			orgData.value.push(data)
			res[0].children.forEach((item) => {
				let data = {
					value: item.id,
					label: item.name
				}
				orgData.value.push(data)
			})
		})
	}
	// 获取我的社团
	const getGroup = () => {
		bizUserGroupApi.bizGroupList().then((res) => {
			res.forEach((item) => {
				groupData.value.push({
					value: item.groupId,
					label: item.groupName
				})
			})
		})
	}

	onMounted(() => {
		getTree()
		getGroup()
	})
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				console.log(formData)
				bizActivityApi
					.bizActivitySubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
